import 'dart:io';

import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import '../../helper/channelHelper.dart';
import '../helper/Helper.dart';

class CameraCardPage extends StatefulWidget {
  @override
  CameraCardPage(this.s1, this.title);
  final onStrChanged s1;
  final String title;
  @override
  createState() => new SignatureInfoPageState();
}

class SignatureInfoPageState extends State<CameraCardPage> {
  late CameraDescription camera;

  late CameraController _controller;
  //Future<void> _initializeControllerFuture;

  @override
  void initState() {
    super.initState();
    init();
  }

  void init() async {
    final cameras = await availableCameras();
    // Get a specific camera from the list of available cameras.
    camera = cameras.first;
    _controller = CameraController(
      // Get a specific camera from the list of available cameras.
      camera,
      // Define the resolution to use.
      ResolutionPreset.high,
    );
    if (!_controller.value.isInitialized) {
      _controller.initialize().then((_) {
        if (!mounted) {
          return;
        }
        setState(() {});
      });
      // Next, initialize the controller. This returns a Future.
      // _initializeControllerFuture = _controller.initialize();
    }
  }

  @override
  void dispose() {
    // Dispose of the controller when the widget is disposed.
    _controller.dispose();
    super.dispose();
  }

  Future upload(BuildContext context, String path) async {
    var _img = File(path);
    if (_img == null) return;
    if (!_img.existsSync()) return;
    var xx = await Helper.ImageToBase64(_img);
    widget.s1(xx);
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return _controller == null
        ? Center(child: CircularProgressIndicator())
        : Scaffold(
            appBar: AppBar(title: Text(widget.title)),
            // Wait until the controller is initialized before displaying the
            // camera preview. Use a FutureBuilder to display a loading spinner
            // until the controller has finished initializing.
            body: CameraPreview(
              _controller,
            ),
            floatingActionButtonLocation:
                FloatingActionButtonLocation.centerFloat,
            floatingActionButton: FloatingActionButton(
              mini: false,

              child: Icon(Icons.camera_alt),
              // Provide an onPressed callback.
              onPressed: () async {
                // Take the Picture in a try / catch block. If anything goes wrong,
                // catch the error.
                try {
                  //final path = Helper.GetRandPath() + ".png";
                  var path = await _controller.takePicture();
                  upload(context, path.path);
                } catch (e) {
                  ChannelHelper.showToast(e.toString());
                }
              },
            ),
          );
  }
}
